package com.wl.org.service.impl;

import com.wl.basic.service.impl.BaseServiceImpl;
import com.wl.basic.util.PageList;
import com.wl.org.domain.Department;
import com.wl.org.mapper.DepartmentMapper;
import com.wl.org.query.DepartmentQuery;
import com.wl.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {

    //注入mapper
    @Autowired
    private DepartmentMapper departmentMapper;

    /*@Override
    //默认只读为false，默认传播机制为REQUIRED[有且仅有一个事务]
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }

    @Override
    public Department loadById(Long id) {
        return departmentMapper.loadById(id);
    }

    @Override
    public List<Department> loadAll() {
        return departmentMapper.loadAll();

    }*/

   /* @Override
    public PageList<Department> queryPage(BaseQuery baseQuery) {
        return null;
    }*/

   /* @Override
    public PageList<Department> queryPage(DepartmentQuery departmentQuery) {
        //查询总条数
        Integer totals = departmentMapper.queryCount(departmentQuery);
        if (totals == 0) {
            return new PageList<>();
        }
        List<Department> data = departmentMapper.queryData(departmentQuery);
        return new PageList<>(totals, data);
    }*/
/*

    @Override
    public void removeA(List<Long> ids) {
        departmentMapper.removeA(ids);
    }
*/

    @Override
    public List<Department> deptTree() {
        // 1. 一次性查询出所有数据，放到内存中，可以使用map
        // 2. 从所有数据中找出第一层级/顶级部门：这就是我们需要返回的数据
        // 3. 遍历所有部门只要不是第一层级的，找自己的上级，并将自己放入上级的children中
        //将所有的部门撞到map中  - 每次获取父部门都从这里获取
        List<Department> all = departmentMapper.loadAll();
        Map<Long, Department> map = new HashMap<>();
        for (Department department : all) {
            map.put(department.getId(), department);
        }

        //前端需要得到的集合数据 - 部门树
        List<Department> deptTree = new ArrayList<>();
        for (Department department : all) {
            if (department.getParent_id() == null) {//顶级部门
                deptTree.add(department);
            } else {//不是顶级部门
                //1.想办法找到当前部门的 父部门
                //Long parent_id = department.getParent_id();
                //这种从数据库查询的方式，每次通过id去查询 - 查询出来映射给实体domain，children集合中都会是空集合 - 没有任何数据
                //Department parentDepartment = departmentMapper.loadById(parent_id);
                //下面这种是从Map中去获取的 - 以前装进去的对象还是会存在
                Department parentDepartment = map.get(department.getParent_id());
                //2.将自己装到  父部门 的children中
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
